package com.pig4cloud.pig.demo.Service.impl;

import com.alibaba.fastjson.JSONObject;
import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.pig4cloud.pig.demo.Mapper.UserMapper;
import com.pig4cloud.pig.demo.Service.UserService;
import com.pig4cloud.pig.demo.feign.Entity.LoginForm;
import com.pig4cloud.pig.demo.feign.Entity.User;
import com.pig4cloud.pig.demo.feign.Entity.UserInfo;
import com.pig4cloud.pig.demo.feign.Util.JWTUtil;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {
    @Autowired
    private UserMapper userMapper;

    @Override
    public List<User> findAll() {
        return userMapper.selectList(null);
    }

    @Override
    public int insert(User user) {
        return userMapper.insert(user);
    }

    @Override
    public String login(LoginForm loginForm) {
        if (StringUtils.isEmpty(loginForm.getStr1())
                || StringUtils.isEmpty(loginForm.getStr2()))
            return null;

        User user = userMapper.login(loginForm.getStr1(), loginForm.getStr2());

        if (user == null)
            return null;

        Map<String,String> map=new HashMap<>();
        map.put("username",user.getUsername());
        map.put("id",user.getId().toString());

        String token = JWTUtil.getToken(map);
        return token;
    }

    @Override
    public int deleteById(Long deleted) {
        return userMapper.deleteById(deleted);
    }

    @Override
    public boolean msgSend(String phoneNumber, Map<String, String> param) {
        if (StringUtils.isEmpty(phoneNumber))
            return false;

        DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", "LTAI4G4QTxrZ44PQxuoQ6amU", "4mQpJ3yd4dPxK8BQ8lVlukqy5KbI5x");
        IAcsClient client = new DefaultAcsClient(profile);

        CommonRequest request = new CommonRequest();
        request.setSysMethod(MethodType.POST);
        request.setSysDomain("dysmsapi.aliyuncs.com");
        request.setSysVersion("2017-05-25");
        request.setSysAction("SendSms");
        request.putQueryParameter("RegionId", "cn-hangzhou");
        request.putQueryParameter("PhoneNumbers", "18588768970");
        request.putQueryParameter("SignName", "文章商城");
        request.putQueryParameter("TemplateCode", "SMS_205055171");
        request.putQueryParameter("TemplateParam", JSONObject.toJSONString(param));
        try {
            CommonResponse response = client.getCommonResponse(request);
            System.out.println(response.getData());
            return response.getHttpResponse().isSuccess();
        } catch (ServerException e) {
            e.printStackTrace();
        } catch (ClientException e) {
            e.printStackTrace();
        }
        return false;
    }

    public User findPhoneNumber(String phoneNumber) {
        QueryWrapper<User> wrapper = new QueryWrapper<>();

        wrapper.eq("phone", Long.parseLong(phoneNumber));

        return userMapper.selectOne(wrapper);
    }

    public User findById(Long id){
        return userMapper.selectById(id);
    }


    //修改密码
    public UserInfo updatePwd(String id,String oldPwd,String newPwd) {
        UserInfo userInfo=new UserInfo();
        User user = userMapper.selectById(id);
        System.out.println(user);
        System.out.println("前端传来的旧密码"+oldPwd);
        System.out.println("数据库里面的旧密码"+user.getPassword());
        if (!oldPwd.equals(user.getPassword())) {
            System.out.println("旧密码不一致");
            userInfo.setStatus(false);
            return userInfo;
        } else {
            System.out.println("旧密码一致");
            user.setUpdatedPwdtime(new Date());
            user.setPassword(newPwd);
            userMapper.updateById(user);
            userInfo.setStatus(true);
            userInfo.setUser(user);
            return userInfo;
        }
    }

    //修改手机号
    public void updatePhone(String id,String phone){
        User user = userMapper.selectById(id);
        Long myphone=Long.parseLong(phone);
        user.setPhone(myphone);
        userMapper.updateById(user);
    }

    //修改邮箱号
    public void updateEmail(String id,String email){
        User user = userMapper.selectById(id);
        user.setEmail(email);
        userMapper.updateById(user);
    }

}
